home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / graphic import.export / completed lab / scaleandrotate.c < prev   
Encoding:
Text File  |  2000-10-06  |  4.5 KB  |  129 lines

  1. // Graphics Importer and Exporter Samples
  2. // This example uses graphics importers to draw an image
  3. // then demonstrates how to perform scaling, rotation and perspective
  4. // manipulation using matrix operations
  5. // Originally written by Sam Bushell for QuickTime "Live" '99
  6. // WWDC 2000 Introduction to QuickTime
  7.  
  8. #include "MacShell.h"
  9.  
  10. void ScaleAndRotate( void )
  11. {
  12.     OSErr err = noErr;
  13.     Handle hOpenTypeList = NewHandle(0);
  14.     long     numTypes = 0;
  15.     FSSpec    theFSSpec;
  16.     GraphicsImportComponent importer = 0;
  17.     Rect naturalBounds, windowBounds, scaledBounds;
  18.     WindowPtr window = NULL;
  19.     MatrixRecord matrix;
  20.     Fixed naturalWidth, naturalHeight;
  21.     FixedPoint fromQuad[4], toQuad[4];
  22.  
  23.     BuildGraphicsImporterValidFileTypes( hOpenTypeList, &numTypes );
  24.     HLock( hOpenTypeList );
  25.     
  26.     err = GetOneFileWithPreview(numTypes, (OSTypePtr)*hOpenTypeList, &theFSSpec, NULL);
  27.     DisposeHandle( hOpenTypeList );
  28.     if ( err ) return;
  29.     
  30.     // locate and open a graphics importer component
  31.     err = GetGraphicsImporterForFile( &theFSSpec, &importer );
  32.     
  33.     // get the native size of the image associated with the importer
  34.     err = GraphicsImportGetNaturalBounds( importer, &naturalBounds );
  35.     
  36.     windowBounds = naturalBounds;
  37.     OffsetRect( &windowBounds, 10, 45 );
  38.     window = NewCWindow( NULL, &windowBounds, "\pScale and Rotate ", true, documentProc, (WindowPtr)-1, true, 0);
  39.     
  40.     // set the graphics port for drawing
  41.     err = GraphicsImportSetGWorld( importer, GetWindowPort( window ), NULL );
  42.     
  43.     // draw the image
  44.     err = GraphicsImportDraw( importer );
  45.     
  46.     pause();
  47.     
  48.     // scale the image by a factor of 2x.  
  49.     // the top-left coordinate of an image's natural bounds is always (0,0).
  50.     scaledBounds = naturalBounds;
  51.     scaledBounds.right = scaledBounds.right * 2;
  52.     scaledBounds.bottom = scaledBounds.bottom * 2;
  53.     
  54.     // define the rectangle in which to draw an image
  55.     err = GraphicsImportSetBoundsRect( importer,        // importer instance
  56.                                        &scaledBounds ); // desired bounds
  57.     
  58.     // resize the window to fit, and redraw.
  59.     SizeWindow( window, scaledBounds.right, scaledBounds.bottom, false );
  60.     err = GraphicsImportDraw( importer );
  61.     
  62.     pause();
  63.     
  64.     // rotate the image ninety degrees clockwise
  65.     // set the contents of a matrix so that it performs no transformation
  66.     SetIdentityMatrix( &matrix );
  67.     
  68.     // modify the contents of a matrix so that it defines a rotation operation
  69.     // 90 degrees to the right - anchor at 0.0 top-left 
  70.     RotateMatrix( &matrix,            // pointer to the matrix structure
  71.                   Long2Fix( 90 ),    // the number of degrees of rotation NOTE: THIS IS A FIXED VALUE
  72.                   0,                // x coordinate of anchor point
  73.                   0 );                // y coordinate of anchor point
  74.     
  75.           
  76.     // we need to return the top-left corner of the rotated image back to it's
  77.     // origin, so add a translation value to a specified matrix 
  78.     TranslateMatrix( &matrix,                            // pointer to the matrix structure
  79.                      Long2Fix( naturalBounds.bottom ),    // deltaH - value added to the x coordinate NOTE: FIXED VALUES
  80.                      0 );                                // deltaV - value added to the y coordinate 
  81.     
  82.     // set the transformation matrix to use for drawing an image             
  83.     err = GraphicsImportSetMatrix( importer, &matrix );
  84.     
  85.     // resize the window to fit, and redraw.
  86.     // get the new bounds rect, size the window then draw
  87.     err = GraphicsImportGetBoundsRect( importer, &scaledBounds );
  88.     SizeWindow( window, scaledBounds.right, scaledBounds.bottom, false );
  89.     err = GraphicsImportDraw( importer );
  90.     
  91.     pause();
  92.     
  93.     // give the image some hefty perspective.
  94.     // set up two quadrilateral coordinates and create a translation matrix
  95.     // from one to the other using QuadToQuadMatrix()
  96.     // QuadToQuadMatrix was added in QuickTime 4.0.
  97.     naturalWidth = Long2Fix( naturalBounds.right );
  98.     naturalHeight = Long2Fix( naturalBounds.bottom );
  99.     fromQuad[0].x = 0;
  100.     fromQuad[0].y = 0;
  101.     fromQuad[1].x = naturalWidth;
  102.     fromQuad[1].y = 0;
  103.     fromQuad[2].x = naturalWidth;
  104.     fromQuad[2].y = naturalHeight;
  105.     fromQuad[3].x = 0;
  106.     fromQuad[3].y = naturalHeight;
  107.     toQuad[0].x = naturalWidth/8;
  108.     toQuad[0].y = naturalHeight/4;
  109.     toQuad[1].x = naturalWidth*9/8;
  110.     toQuad[1].y = 0;
  111.     toQuad[2].x = naturalWidth;
  112.     toQuad[2].y = naturalHeight*6/4;
  113.     toQuad[3].x = 0;
  114.     toQuad[3].y = naturalHeight;
  115.     SetIdentityMatrix( &matrix );
  116.     QuadToQuadMatrix( (Fixed *)fromQuad, (Fixed *)toQuad, &matrix );
  117.     err = GraphicsImportSetMatrix( importer, &matrix );
  118.     
  119.     // resize the window to fit, and redraw
  120.     // get the new bounds rect, size the window then draw
  121.     err = GraphicsImportGetBoundsRect( importer, &scaledBounds );
  122.     SizeWindow( window, scaledBounds.right, scaledBounds.bottom, false );
  123.     SetPortWindowPort( window );
  124.     EraseRect( &scaledBounds );
  125.     err = GraphicsImportDraw( importer );
  126.  
  127.     CloseComponent( importer );
  128. }
  129.